package club.kingyin.component;

import club.kingyin.core.BeanDefinition;
import club.kingyin.core.Scope;

import java.io.File;

/**
 * @author ：kingyin
 * @date ：创建于 2021/7/17 5:43 下午
 * @description ：默认BeanDefinition
 * @modified By：
 * @version: 1.0.0
 */
public class DefaultBeanDefinition implements BeanDefinition {

    private String beanClassName;
    private Scope scope;
    private Class<?> classType;

    @Override
    public void setBeanClassName(String beanClassName) {
        try {
            // 加载类
            this.classType = ClassLoader.getSystemClassLoader().loadClass(beanClassName);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }

        String[] split = beanClassName.split("\\.");

        StringBuilder sb = new StringBuilder(split[split.length-1]);

        // 把第一个字符转换成小写
        sb.setCharAt(0,String.valueOf(sb.charAt(0)).toLowerCase().toCharArray()[0]);

        this.beanClassName = sb.toString();
    }

    @Override
    public String getBeanClassName() {
        return beanClassName;
    }

    @Override
    public void setScope(Scope scope) {
        this.scope = scope;
    }

    @Override
    public Scope getScope() {
        return scope;
    }

    @Override
    public boolean isSingleton() {
        return scope == Scope.SINGLETON;
    }

    @Override
    public boolean isPrototype() {
        return scope == Scope.PROTOTYPE;
    }

    @Override
    public Class<?> getBeanClass() {
        return classType;
    }

}
